package biz.inspeed.test.url;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

public class DownloadJPG {

	private static void downloadURL(String url) throws Exception {
		int lastIndexOf = url.indexOf("cn");
		String b_path = url.substring(lastIndexOf);
		int indexOf = b_path.indexOf("/");
		String dir = b_path.substring(0, indexOf);
		// System.out.println(dir);
		String baseDir = "E:\\xx-python\\pic";
		String left = b_path.substring(indexOf + 1);
		String replace = left.replace('/', '.');
		System.out.println(replace);

		File f_dir = new File(baseDir, dir);
		if (!f_dir.exists()) {
			f_dir.mkdirs();
		}

		File f_jpg = new File(f_dir, replace);
		if (f_jpg.exists()) {
			System.out.println("已经下载了--->" + url);
			return;
		}
		FileOutputStream out = new FileOutputStream(f_jpg);
		URLConnection connect = new URL(url).openConnection();
		// connect.addRequestProperty("User-Agent",
		// "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
		connect.setRequestProperty(
				"User-Agent",
				"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
		InputStream inputStream = connect.getInputStream();
		download(inputStream, out);
		System.out.println("downloaded-->" + url);
	}

	private static void download(InputStream in, OutputStream out)
			throws Exception {
		byte[] buf = new byte[1024 * 1024];
		int len = 0;
		while ((len = in.read(buf)) != -1) {
			out.write(buf, 0, len);
		}
		out.flush();
		out.close();
		in.close();
	}

	private static void readLines() throws Exception {
		File f = new File("E:\\xx-python\\jpg1.txt");
		BufferedReader br = new BufferedReader(new FileReader(f));
		String l = null;
		while ((l = br.readLine()) != null) {
			// System.out.println(l);
			try {
				downloadURL(l);
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("exception --->" + l);
			}
		}
	}

	private static void use_http_header() throws Exception {
		String url = "http://www.baidu.com/";
		String proxy = "127.0.0.1";
		String    port = "8087";
		Properties systemProperties = System.getProperties();
		systemProperties.setProperty("http.proxyHost",proxy);
		systemProperties.setProperty("http.proxyPort",port);
		// HttpURLConnection httpURLConnection = new
		URLConnection connect = new URL(url).openConnection();
		// connect.addRequestProperty("User-Agent",
		// "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
		HttpURLConnection httpConnection = (HttpURLConnection) connect;
		httpConnection
				.setRequestProperty(
						"User-Agent",
						"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
		System.out.println(httpConnection.getRequestProperties());
		InputStream in = httpConnection.getInputStream();
		int len = -1;
		byte[] buf = new byte[8 * 10];
		while ((len = in.read(buf)) != -1) {
			for (int i = 0; i < len; i++) {
				System.out.print(buf[i]);
			}
			System.out.println();
		}
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String proxy = "127.0.0.1";
		String    port = "8087";
		Properties systemProperties = System.getProperties();
		systemProperties.setProperty("http.proxyHost",proxy);
		systemProperties.setProperty("http.proxyPort",port);
		readLines();
		// downloadURL("http://img60.eeeimg.com/xtu/img/cn1/003/5.jpg");
//		 use_http_header();
	}

}
